package com.wqj.kyee.gps.ui;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.romainpiel.shimmer.Shimmer;
import com.romainpiel.shimmer.ShimmerTextView;
import com.wqj.kyee.gps.R;
import com.wqj.kyee.gps.service.NMEAParserService;

import java.util.ArrayList;
import java.util.List;

import me.weyye.hipermission.HiPermission;
import me.weyye.hipermission.PermissionCallback;
import me.weyye.hipermission.PermissionItem;

public class SplashScreen extends Activity {

    private static final String TAG = SplashScreen.class.getSimpleName();

    ShimmerTextView tv;
    Shimmer shimmer;

    private final int SPLASH_DISPLAY_TIME = 2000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);

        tv = (ShimmerTextView) findViewById(R.id.shimmer_tv);

        shimmer = new Shimmer();
        shimmer.start(tv);
    }

    @Override
    protected void onResume() {
        super.onResume();

        List<PermissionItem> permissionItems = new ArrayList<>();
        permissionItems.add(new PermissionItem(Manifest.permission.READ_PHONE_STATE, "手机状态", R.drawable.permission_ic_phone));
        permissionItems.add(new PermissionItem(Manifest.permission.ACCESS_FINE_LOCATION, "位置信息", R.drawable.permission_ic_location));
        permissionItems.add(new PermissionItem(Manifest.permission.WRITE_EXTERNAL_STORAGE, "存储", R.drawable.permission_ic_storage));
        HiPermission.create(SplashScreen.this)
                .title(getString(R.string.permission_cus_title))
                .permissions(permissionItems)
                .msg(getString(R.string.permission_cus_msg))
                .animStyle(R.style.PermissionAnimScale)
                .style(R.style.PermissionDefaultBlueStyle)
                .checkMutiPermission(new PermissionCallback() {
                    @Override
                    public void onClose() {
                        Log.i(TAG, "onClose");
                        finish();
                    }

                    @Override
                    public void onFinish() {
                        Log.i(TAG, "onFinish");
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {

                                Intent intent = new Intent(SplashScreen.this, NMEAParserService.class);
                                startService(intent);

                                Intent main = new Intent(SplashScreen.this, MainActivity.class);
                                startActivity(main);
                                finish();
                            }
                        }, SPLASH_DISPLAY_TIME);
                    }

                    @Override
                    public void onDeny(String permission, int position) {
                        Log.i(TAG, "onDeny");
                    }

                    @Override
                    public void onGuarantee(String permission, int position) {
                        Log.i(TAG, "onGuarantee");
                    }
                });

        /*
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent main = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(main);
                finish();
            }
        }, SPLASH_DISPLAY_TIME);
        */
    }
}
